/**
 * Copyright 2004 - 2006  Blue Bamboo International Inc. 
 *           All rights reserved.
 *
 *
 *  Blue Bamboo PROPRIETARY/CONFIDENTIAL.
 *
 */

/** 
 * File name:            ShowContextMenu.java
 * 
 * Originally developed: Nick.Tan
 *
 * Create date :         Jul 18, 2007
 * 
 * Description:          The demo of the header on evey java code
 *                       These header could be configured in the Eclipse
 *                       The Eclipse template will come out next. 
 * 
 * Version:              0.1
 * 
 * Contributors:         
 * 
 * Modifications: 
 * name          version           reasons
 * 
 * $Log:$
 */
package com.googlecode.eclipse.navigatorext.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.eclipse.jdt.internal.core.PackageFragment;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.jcommander.systemshell.SystemShell;

/**
 * 
 */
public class ShowContextMenu implements IViewActionDelegate, IEditorActionDelegate, IObjectActionDelegate {

    private IViewPart viewPart;
    private IEditorPart editorPart;
    private ISelection selection;
    private List<File> fileList = new ArrayList<File>();

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     */
    public void run(IAction action) {
        fileList.clear();
        if (viewPart != null) {
            IStructuredSelection ssl = (IStructuredSelection) selection;
            for (Iterator it = ssl.iterator(); it.hasNext();) {
                IAdaptable adaptable = (IAdaptable) it.next();
                File file = null;
                if (adaptable instanceof IResource) {
                    file = new File(((IResource) adaptable).getLocation().toOSString());
                } else if (adaptable instanceof PackageFragment
                        && ((PackageFragment) adaptable).getPackageFragmentRoot() instanceof JarPackageFragmentRoot) {
                    file = getJarFile(((PackageFragment) adaptable).getPackageFragmentRoot());
                } else if (adaptable instanceof JarPackageFragmentRoot) {
                    file = getJarFile(adaptable);
                } else {
                    file = new File(((IResource) adaptable.getAdapter(IResource.class)).getLocation().toOSString());
                }
                fileList.add(file);
            }
        } else if (editorPart != null) {
            File file = new File(getFile(editorPart.getEditorInput()).getLocation().toOSString());
            fileList.add(file);
        }
        if (fileList.size() > 0)
            SystemShell.showContextMenu((File[]) fileList.toArray(new File[fileList.size()]), -1, -1);
    }

    public void init(IViewPart view) {
        this.viewPart = view;
        this.editorPart = null;
    }

    public void selectionChanged(IAction action, ISelection selection) {
        this.selection = selection;
    }

    public void setActiveEditor(IAction action, IEditorPart targetEditor) {
        this.editorPart = targetEditor;
        this.viewPart = null;
    }

    protected File getJarFile(IAdaptable adaptable) {
        JarPackageFragmentRoot jpfr = (JarPackageFragmentRoot) adaptable;
        File selected = (File) jpfr.getPath().makeAbsolute().toFile();
        if (!((File) selected).exists()) {
            File projectFile = new File(jpfr.getJavaProject().getProject().getLocation().toOSString());
            selected = new File(projectFile.getParent() + selected.toString());
        }
        return selected;
    }

    private IFile getFile(IEditorInput input) {
        if (input == null) {
            return null;
        }
        Object adapter = input.getAdapter(IFile.class);
        if (adapter instanceof IFile) {
            return (IFile) adapter;
        }
        adapter = getAdapter(IFile.class);
        if (adapter instanceof IFile) {
            return (IFile) adapter;
        }
        return null;
    }

    private Object getAdapter(Class clazz) {
        if (editorPart == null) {
            return null;
        }
        return editorPart.getAdapter(clazz);
    }

    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        if (targetPart instanceof IViewPart) {
            viewPart = (IViewPart) targetPart;
            editorPart = null;
        } else if (targetPart instanceof IEditorPart) {
            editorPart = (IEditorPart) targetPart;
            viewPart = null;
        }
    }
}
